package com.ruoyi.book.service.impl;

import java.util.List;

import com.ruoyi.category.domain.Category;
import com.ruoyi.category.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.book.mapper.BookMapper;
import com.ruoyi.book.domain.Book;
import com.ruoyi.book.service.IBookService;

/**
 * 图书信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-21
 */
@Service
public class BookServiceImpl implements IBookService
{
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询图书信息
     *
     * @param id 图书信息主键
     * @return 图书信息
     */
    @Override
    public Book selectBookById(Long id)
    {
        Book book = bookMapper.selectBookById(id);
        Integer up=book.getGrounding();
        Integer recommend=book.getRecommend();
        if(up.equals(1)){
            book.setUp("是");
        }else{
            book.setUp("否");
        }
        if(recommend.equals(1)){
            book.setTuijian("是");
        }else{
            book.setTuijian("否");
        }

        return book;
    }

    /**
     * 查询图书信息列表
     *
     * @param book 图书信息
     * @return 图书信息
     */
    @Override
    public List<Book> selectBookList(Book book)
    {
        List<Book> bookList = bookMapper.selectBookList(book);
        for(Book book1:bookList){
            Integer up =book1.getGrounding();
            Integer recommend=book1.getRecommend();
            if(up.equals(1)){
                book1.setUp("是");
            }else{
                book1.setUp("否");
            }
            if(recommend.equals(1)){
                book1.setTuijian("是");
            }else{
                book1.setTuijian("否");
            }
        }
        return bookList;
    }

    /**
     * 新增图书信息
     *
     * @param book 图书信息
     * @return 结果
     */
    @Override
    public int insertBook(Book book)
    {
        List<Category> categoryList =categoryMapper.selectCategoryList(null);
        for(Category category:categoryList){
            if(category.getName().equals(book.getCategoryName())){
                book.setCategoryId(category.getId());
            }
        }
        if(book.getUp().equals("是")){
            book.setGrounding(1);
        }else{
            book.setGrounding(0);
        }
        if(book.getTuijian().equals("是")){
            book.setRecommend(1);
        }else{
            book.setRecommend(0);
        }
        return bookMapper.insertBook(book);
    }

    /**
     * 修改图书信息
     *
     * @param book 图书信息
     * @return 结果
     */
    @Override
    public int updateBook(Book book)
    {
        List<Category> categoryList =categoryMapper.selectCategoryList(null);
        for(Category category:categoryList){
            if(category.getName().equals(book.getCategoryName())){
                book.setCategoryId(category.getId());
            }
        }
        if(book.getUp().equals("是")){
            book.setGrounding(1);
        }else{
            book.setGrounding(0);
        }
        if(book.getTuijian().equals("是")){
            book.setRecommend(1);
        }else{
            book.setRecommend(0);
        }
        return bookMapper.updateBook(book);
    }

    /**
     * 批量删除图书信息
     *
     * @param ids 需要删除的图书信息主键
     * @return 结果
     */
    @Override
    public int deleteBookByIds(Long[] ids)
    {
        return bookMapper.deleteBookByIds(ids);
    }

    /**
     * 删除图书信息信息
     *
     * @param id 图书信息主键
     * @return 结果
     */
    @Override
    public int deleteBookById(Long id)
    {
        return bookMapper.deleteBookById(id);
    }
}
